package com.douguo.recipe;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.douguo.recipe.SettingActivity.SettingItem;
import com.douguo.recipe.manager.Registry;
import com.douguo.recipe.manager.SharedPreferencesManager;
import com.douguo.recipe.network.Constant;
import com.douguo.recipe.network.Util;
import com.douguo.recipe.network.Util.OnNetworkCheckProgress;
import com.douguo.recipe.view.NavigationView;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SettingNetworkActivity extends DouguoActivity {
	
	private static final int DIALOG_PROGRESS = 0;
	private static final int MAX_PROGRESS = 100;
	private static final int MSG_PROGRESS = 0;
	private static final int MSG_DIMISSDIALOG = 1;
	private static final int MSG_SHOWTOAST = 2;
	private static final int MSG_ARG1_SUCCESS = 0;
	private static final int MSG_ARG1_FAILURE = 1;
	
	// --------------ui--------------
	private NavigationView mNavigationView;
	private ListView mLvSetting;
	private ProgressDialog mProgressDialog;
	
	// -------------adatper--------------
	private SettingAdapter mAdapter;
	
	// -------------data-------------
	private List<SettingItem> mSettingList;
	
	private Thread mCheckNetworkThread;
	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_PROGRESS:
				mProgressDialog.setProgress(msg.arg1);
				break;
			case MSG_DIMISSDIALOG:

				break;
			case MSG_SHOWTOAST:
				if (msg.arg1 == MSG_ARG1_SUCCESS) {
					Util.showShortToast(SettingNetworkActivity.this,
							R.string.checkNetworkSuccess);
				} else if (msg.arg1 == MSG_ARG1_FAILURE) {
					Util.showShortToast(SettingNetworkActivity.this,
							R.string.checkNetworkFailure);
				}
				break;
			}
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settingnetwork);
		initData();
		initUI();
	}
	
	private void initUI() {
		mNavigationView = new NavigationView(this, NavigationView.STATE_MORE);
		mLvSetting = (ListView) findViewById(R.id.setting_network_list);
		mLvSetting.setAdapter(mAdapter);
		mLvSetting.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				SettingItem item = (SettingItem) mAdapter.getItem(position);
				item.onClickListener.onClick(view);
			}
		});
		findViewById(R.id.imageButton_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				
			}
		});
		setTitle(getString(R.string.titleWangLuoSheZhi));
	}
	
	private void initData() {
		mAdapter = new SettingAdapter();
		mSettingList= new ArrayList<SettingItem>();
		
		initSettingItems();
	}
	
	private void initSettingItems() {
		Registry registry = Registry.getInstance();
//		final SharedPreferencesManager manager = (SharedPreferencesManager) registry.get(Constant.PREFERENES_MANAGER);
		boolean isUseNetwork = Util.isUseNetworkPreferences(this);
		boolean isDownloadThumbnail = Util.isDownloadPhotoPreferences(this);
//		boolean isDownloadImage = manager.getBoolean(Constant.PREFERENCES_KEY_DOWNLOADIMAGE);
		final SettingItem useNetworkItem = new SettingItem();//是否使用网络
		final SettingItem downloadThumbnailItem = new SettingItem();//是否自动下载缩略图
//		final SettingItem downloadImageItem = new SettingItem();//是否自动下载大图
		final SettingItem checkNetworkItem = new SettingItem();//检查网络
		
		useNetworkItem.title = getString(R.string.useNetwork);
		useNetworkItem.type =  SettingItem.TYPE_SELECT;
		useNetworkItem.isSelect = isUseNetwork;
		useNetworkItem.isEnable = true;
		useNetworkItem.onClickListener = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				useNetworkItem.isSelect = !useNetworkItem.isSelect;
				downloadThumbnailItem.isEnable = useNetworkItem.isSelect;
//				downloadImageItem.isEnable = useNetworkItem.isSelect;
//				manager.putBoolean(Constant.PREFERENCES_KEY_USENETWORK,useNetworkItem.isSelect);
				Util.setUseNetworkPreferences(SettingNetworkActivity.this, useNetworkItem.isSelect);
				if(!useNetworkItem.isSelect) {
					mSettingList.remove(downloadThumbnailItem);
//					mSettingList.remove(downloadImageItem);
				}
				else {
					mSettingList.add(1,downloadThumbnailItem);
//					mSettingList.add(2,downloadImageItem);
				}
				mAdapter.notifyDataSetChanged();
			}
		};
		
		downloadThumbnailItem.title = getString(R.string.autoDownloadThumb);
		downloadThumbnailItem.type =  SettingItem.TYPE_SELECT;
		downloadThumbnailItem.isSelect = isDownloadThumbnail;
		downloadThumbnailItem.isEnable = useNetworkItem.isSelect;
		downloadThumbnailItem.onClickListener = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(downloadThumbnailItem.isEnable) {
					downloadThumbnailItem.isSelect = !downloadThumbnailItem.isSelect;
					Util.setDownloadPhotoPreferences(SettingNetworkActivity.this, downloadThumbnailItem.isSelect);
//					manager.putBoolean(Constant.PREFERENCES_KEY_DOWNLOADTHUMBNARIL,downloadThumbnailItem.isSelect);
					mAdapter.notifyDataSetChanged();
				}
			}
		};
//		downloadImageItem.title = getString(R.string.autoDownloadPic);
//		downloadImageItem.type =  SettingItem.TYPE_SELECT;
//		downloadImageItem.isSelect = isDownloadImage;
//		downloadImageItem.isEnable = useNetworkItem.isSelect;
//		downloadImageItem.onClickListener = new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				if(downloadImageItem.isEnable) {
//					downloadImageItem.isSelect = !downloadImageItem.isSelect;
//					manager.putBoolean(Constant.PREFERENCES_KEY_DOWNLOADIMAGE,downloadImageItem.isSelect);
//					mAdapter.notifyDataSetChanged();
//				}
//			}
//		};
		
		checkNetworkItem.title = getString(R.string.settingNetwork);
		checkNetworkItem.type = SettingItem.TYPE_CLICK_ACTION;
		checkNetworkItem.onClickListener = getCheckNetworkOnclickListener();
		
	
		
		mSettingList.add(useNetworkItem);
		if(downloadThumbnailItem.isEnable) {
			mSettingList.add(downloadThumbnailItem);
		}
//		if(downloadImageItem.isEnable){
//			mSettingList.add(downloadImageItem);
//		}
		mSettingList.add(checkNetworkItem);

	}
	
	class SettingItem {
		final static int TYPE_SELECT = 0;
//		final static int TYPE_INTENT = 1;
		final static int TYPE_CLICK_ACTION = 2;
		String title;
		int type;
		boolean isSelect;
		boolean isEnable;
		Intent intent;
		OnClickListener onClickListener;
	}
	
	
	private OnClickListener getCheckNetworkOnclickListener() {
		return new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(DIALOG_PROGRESS);
				mCheckNetworkThread = new Thread() {
					public void run() {
						boolean flag = Util.checkNetwork(SettingNetworkActivity.this,
											new OnNetworkCheckProgress() {
	
												public void update(int progressValue) {
													mHandler.sendMessage(mHandler
															.obtainMessage(
																	MSG_PROGRESS,
																	progressValue,
																	0));
												}
											});
						int arg1 = MSG_ARG1_FAILURE;
						if (flag == true) {
							arg1 = MSG_ARG1_SUCCESS;
						}
						if (!isInterrupted()) {
							mHandler.sendMessage(mHandler.obtainMessage(MSG_SHOWTOAST, arg1,0));
							mProgressDialog.dismiss();
						}
					}
				};
				mCheckNetworkThread.start();
			}

		};
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_PROGRESS:
			mProgressDialog = new ProgressDialog(SettingNetworkActivity.this);
			// mProgressDialog.setIcon(R.drawable.alert_dialog_icon);
			mProgressDialog.setTitle(R.string.waitNetworkCheck);
			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			mProgressDialog.setMax(MAX_PROGRESS);

			return mProgressDialog;

		}
		return null;
	}
	
	
	class SettingAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mSettingList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mSettingList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			if(convertView  == null) {
				view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.setting_listitem, null);
			}
			TextView textView = (TextView) view.findViewById(R.id.setting_listitem_text);
			SettingItem settingitem = (SettingItem) getItem(position);
			textView.setText(settingitem.title);
			
			ImageView imgselector = (ImageView) view.findViewById(R.id.setting_selector);
			imgselector.setVisibility(View.INVISIBLE);
			
			ImageView imgArrow = (ImageView) view.findViewById(R.id.setting_right_arrow);
			imgArrow.setVisibility(View.INVISIBLE);
			
			int type = settingitem.type;
			switch(type) {
			case SettingItem.TYPE_CLICK_ACTION://单击动作
				imgArrow = (ImageView) view.findViewById(R.id.setting_right_arrow);
				imgArrow.setVisibility(View.VISIBLE);
				break;
//			case SettingItem.TYPE_INTENT://打开新窗口
//				startActivity(settingitem.intent);
//				break;
			case SettingItem.TYPE_SELECT://选项:
				boolean isSelect = settingitem.isSelect;
				imgselector.setImageResource(isSelect == true? R.drawable.selected:R.drawable.notselected);
				imgselector.setVisibility(settingitem.isEnable?View.VISIBLE:View.INVISIBLE);
				imgselector.setOnClickListener(settingitem.onClickListener);
				break;
			}
			return view;
		}
		
	}
}
